<?php 
namespace Admin\Controller;
use Think\Controller;
class AuthorController extends Controller{
		protected $db;
		protected function _initialize(){
			$this->db = D('Author');
		}
		
		public function index(){
			$data = $this->db->getAll();
			foreach($data as $i => $v){
				$arr[$i]['id'] = $v['id'];
				$arr[$i]['authorname'] = $v['authorname'];
				$arr[$i]['authorpic'] = $v['authorpic'];
			}
 
			$this->assign('author',$arr);
			$this->display();
		}
		
		public function del(){
			$id = I('get.id');
			if($this->db->del($id)){
				$this->success('删除成功');
			}else{
				$this->error($this->db->getError());
			}
		}
		
		public function add(){
 
			if(IS_POST){			

				if($this->db->m_add()){
					$this->success('添加成功');
				}else{
					$this->error($this->db->getError());
				}
			}else{
				$this->display();
			}
 
		}
		
		public function edit(){
			
			$id = I('get.id');
			
			$data = $this->db->getAll($id);
 	
			if(IS_POST){		
 
				if($this->db->edit()){
					$this->success('修改成功');
				}else{
					if(empty($this->db->getError())){
						$this->error('修改失败，值相同');
					}else{
						$this->error($this->db->getError());
					}
				}
			}
			$this->assign('id',$id);
			$this->assign('data',$data);
			
			$this->display();
		}
		
		public function pic(){
			$id = I('get.id');
			$data = $this->db->getAll($id);
			$data = $data['authorpic'];
	 
			if(I('get.pid')){
				//删除旧文件
				$this->db->unnk($data);
				//清除authorPic 字段  达到删除效果
				$_POST['authorPic'] = '';
 
				if($this->db->edit($id)){
					$this->success('删除成功');
				}else{
					$this->error($this->db->getError());
				}
			}
	 
			if(IS_POST){

				$pic = $this->db->upload('authorPic','authorPic','头像上传失败');
				//删除旧文件
				$this->db->unnk(I('authorPic'));
				
				$_POST['authorPic'] = $pic[0];
 
				if($this->db->edit(I('post.id'))){
					$this->success('修改成功');
				}else{
					$this->error($this->db->getError());
				}
			}

			$this->assign('id',$id);
			$this->assign('data',$data);
			$this->display();
		}
		
		public function photo(){
			$data = $this->db->getAll(I('param.id'));
 
			$data = json_decode($data['authorimg']);
	 
			for($i=0;$i<count($data);$i++){			
				$arr[$i]['pid'] = $i+1;
				$arr[$i]['img'] = $data[$i];
			}
		 
 
			if(I('GET.pid')){
				$pid = I('GET.pid');
				foreach($arr as $v){
					if($v['pid']!=$pid){
						$ars[] = $v['img'];
					}else{
						$one = $v['img'];
					}
		 
							
				}
				//删除上一个文件
				$this->db->unnk($one);
				
				$_POST['authorImg']=json_encode($ars);			
				if($this->db->edit(I('GET.id'))){
					$this->success('删除成功');
				}else{
					$this->error('删除失败');
				}
			} 
			
			if(IS_POST){
				 
				$img = $this->db->upload('authorImg','authorImg','写真上传失败');
				
				$data[]=$img;
				
				
				
				foreach($data as  $v){
					if(!is_array($v)){
						$datas[]=$v;
					}
					foreach($v as $k){
						$datas[]=$k;
					}
				}
				$_POST['authorImg'] = json_encode($datas);
 
 
				if($this->db->edit(I('post.id'))){
					$this->success('上传写真成功');
				}else{
					$this->error($this->db->getError());
				}
			}
	 
			$this->assign('id',I('GET.id'));
			$this->assign('data',$arr);
			$this->display();
			
				
			 
			
		}
		
		 
		
}